package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://epaper.jwb.com.cn/jwb/html/2012-12/03/node_2.htm 今晚报
 * http://epaper.jwb.com.cn/bhzb/html/2012-12/03/node_242.htm 渤海早报
 * http://epaper.jwb.com.cn/jjzb/html/2012-11/30/node_302.htm 经济周报
 * http://epaper.jwb.com.cn/lnsb/html/2012-12/03/node_222.htm 中老年报
 * @author allen 
 */
public class jwbNewsPaperAnalyse implements AnalyseNews {
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://epaper.jwb.com.cn/jwb/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_.*.htm",
				"http://epaper.jwb.com.cn/bhzb/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_.*.htm",
				"http://epaper.jwb.com.cn/jjzb/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_.*.htm",
				"http://epaper.jwb.com.cn/lnsb/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_.*.htm",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta newspaper = new NewsMeta();
				
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
						
		newspaper.setUrl(url);		
		
		
		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		Document doc = Jsoup.parse(html);
		if(url.matches("http://epaper.jwb.com.cn/jwb/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_.*.htm")){
			title =doc.select("title").text();
			
			title=title.replace("-今晚报", "");
	       
			date=DateUtils.matchDate(url);
			
			content =HtmlCleaner.getContentHtml(url,doc.select("div#container founder-content"));
			
			
			newspaper.setTitle(StringUtils.trimSpace(title));
			newspaper.setContent(StringUtils.trimSpace(content));
			newspaper.setDate(date);
		}else if(url.matches("http://epaper.jwb.com.cn/bhzb/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_.*.htm")){
			title =doc.select("title").text();
			
			title=title.replaceAll("-", "");
			title=title.replace("今晚数字报刊", "");
	       
			date=DateUtils.matchDate(url);
			
			content =HtmlCleaner.getContentHtml(url,doc.select("div#container founder-content"));
			
			
			newspaper.setTitle(StringUtils.trimSpace(title));
			newspaper.setContent(StringUtils.trimSpace(content));
			newspaper.setDate(date);
		}else if(url.matches("http://epaper.jwb.com.cn/jjzb/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_.*.htm")){
			title =doc.select("title").text();
			
			title=title.replaceAll("-", "");
			title=title.replace("今晚经济周报", "");
	       
			date=DateUtils.matchDate(url);
			
			content =HtmlCleaner.getContentHtml(url,doc.select("div#container founder-content"));
			
			
			newspaper.setTitle(StringUtils.trimSpace(title));
			newspaper.setContent(StringUtils.trimSpace(content));
			newspaper.setDate(date);
		}else if(url.matches("http://epaper.jwb.com.cn/lnsb/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_.*.htm")){
			title =doc.select("html body table tbody tr td table tbody tr td table tbody tr td table tbody tr td strong").text();
			
			date=DateUtils.matchDate(url);
			
			content =HtmlCleaner.getContentHtml(url,doc.select("div#ozoom"));
			
			
			newspaper.setTitle(StringUtils.trimSpace(title));
			newspaper.setContent(StringUtils.trimSpace(content));
			newspaper.setDate(date);
		}
		return newspaper;
	}
	
	
	public boolean isTaskPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://epaper.jwb.com.cn/jwb/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/node_.*.htm",
				"http://epaper.jwb.com.cn/bhzb/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/node_.*.htm",
				"http://epaper.jwb.com.cn/jjzb/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/node_.*.htm",
				"http://epaper.jwb.com.cn/lnsb/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/node_.*.htm"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	
	

	
	
}
